﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using BrainTechLLC.WPF;
using System.Drawing;
using System.Windows.Media;
using BrainTechLLC;
using System.Diagnostics;
using BrainTechLLC.ImageOperatorsNonAForgeDependent;
using System.IO;

namespace SimpleFileDataAndViewModels
{
    public class ThumbnailImageProvider : ImageProviderBase, INotifyPropertyChanged, IObjectLock, IImageSourceProvider, IBitmapProvider
    {
        public int _OriginalBitmapWidth;
        public int _OriginalBitmapHeight;
        public int _TargetBitmapWidth;
        public int _TargetBitmapHeight;

        public ThumbnailImageProvider()
            : base()
        {
        }

        public ThumbnailImageProvider(HasBytes fileImage)
            : base(fileImage)
        {
        }

        public int OriginalBitmapWidth
        {
            get { return _OriginalBitmapWidth; }
            set
            {
                if (_OriginalBitmapWidth != value)
                {
                    _OriginalBitmapWidth = value;
                    OnPropertyChanged("OriginalBitmapWidth");
                }
            }
        }

        public int OriginalBitmapHeight
        {
            get { return _OriginalBitmapHeight; }
            set
            {
                if (_OriginalBitmapHeight != value)
                {
                    _OriginalBitmapHeight = value;
                    OnPropertyChanged("OriginalBitmapHeight");
                }
            }
        }

        public int TargetBitmapWidth
        {
            get { return _TargetBitmapWidth; }
            set
            {
                if (_TargetBitmapWidth != value)
                {
                    _TargetBitmapWidth = value;
                    OnPropertyChanged("TargetBitmapWidth");
                }
            }
        }

        public int TargetBitmapHeight
        {
            get { return _TargetBitmapHeight; }
            set
            {
                if (_TargetBitmapHeight != value)
                {
                    _TargetBitmapHeight = value;
                    OnPropertyChanged("TargetBitmapHeight");
                }
            }
        }

        public Bitmap GetThumbnailBitmap()
        {
            Bitmap bmThumb = null;

            byte[] bytes = _Item.Bytes;
            if (bytes == null)
            {
                lock (_lockObject)
                {
                    if (_Item.Bytes == null)
                    {
                        bytes = _Item.Load();
                        _Item.Bytes = bytes;
                    }
                }
            }

            Bitmap bmOriginal = bytes.ReadBitmapFromByteStream();
            OriginalBitmapWidth = bmOriginal.Width;
            OriginalBitmapHeight = bmOriginal.Height;

            try
            {
                bmThumb = CreateAndSetThumbnail(bmOriginal);
                _Bitmap = bmThumb;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            return bmThumb;
        }

        public Bitmap CreateAndSetThumbnail(Bitmap bmOriginal)
        {
            if (bmOriginal == null)
                return null;

            Bitmap bmOut;
            RenderSettings renderSettings;

            if (true) //Settings.Values.HighQualityThumbs)
            {
                renderSettings = new RenderSettings()
                {
                    //PixelFormat = Settings.Values.StandardColorPixelFormat,
                    //Interpolation = Settings.Values.InterpolationMode,
                    //Smoothing = Settings.Values.SmoothingMode,
                    //RenderingMethod = Settings.Values.InternalRendering,
                    //ForceHighQuality = Settings.Values.HighQualityThumbs
                    PixelFormat = System.Drawing.Imaging.PixelFormat.Format24bppRgb,
                    Interpolation = System.Drawing.Drawing2D.InterpolationMode.High,
                    Smoothing = System.Drawing.Drawing2D.SmoothingMode.HighQuality,
                    RenderingMethod = InternalRenderingMethod.None,
                    ForceHighQuality = false
                };
            }

            bmOut = bmOriginal.CreateThumbnail(OriginalBitmapWidth, OriginalBitmapHeight, (int)TargetBitmapWidth, (int)TargetBitmapHeight, renderSettings);
            ActualWidth = bmOut.Width;
            ActualHeight = bmOut.Height;
            return bmOut;
        }


        public override Bitmap Bitmap
        {
            get
            {
                if (_Bitmap == null)
                {
                    lock (_lockObject)
                    {
                        if (_Bitmap == null)
                        {
                            _Bitmap = GetThumbnailBitmap();
                        }
                    }
                }
                return _Bitmap;
            }
        }

        public override ImageSource ImageSource
        {
            get
            {
                if (_ImageSource == null)
                {
                    lock (_lockObject)
                    {
                        if (_ImageSource == null)
                        {
                            Bitmap bitmap = Bitmap;
                            _ImageSource = bitmap.LoadImageSource(_Item.Bytes);
                        }
                    }
                }
                return _ImageSource;
            }
        }
    }
}
